//
//  MineViewController.m
//  LianShangBao
//
//  Created by 主事丫环HU on 2020/8/27.
//  Copyright © 2020 主事丫环. All rights reserved.
//

#import "MineViewController.h"
#import "PersonalInfoModel.h"
#import "InfoManageController.h"// 个人信息管理
#import "CodeLoginController.h"
#import "RechargeController.h"// 充值
#import "WithdrawController.h"// 提现
#import "PickUpOrderController.h"// 提货订单
#import "RequestPickUpController.h"// 申请提货
#import "ShippingAddressController.h"// 收货地址
#import "SafeCenterController.h"// 安全中心
#import "MyMessageController.h"// 消息
#import "MyFriendsController.h"// 我的好友
#import "RecommendFriendsController.h"// 推荐给好友
#import "WithdrawPointsController.h"// 已提现积分
#import "UnWithdrawPointsController.h"// 未提现积分
#import "MoreViewController.h"// 更多
@interface MineViewController ()
@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;
@property (weak, nonatomic) IBOutlet UIImageView *headImageView;// 头像
@property (weak, nonatomic) IBOutlet UILabel *nameLabel;//姓名
@property (weak, nonatomic) IBOutlet UILabel *accountStatusLabel;//账号状态
@property (weak, nonatomic) IBOutlet UIButton *canSeeButton;//可视按钮
@property (weak, nonatomic) IBOutlet UILabel *totalMoneyLabel;//总资产
@property (weak, nonatomic) IBOutlet UILabel *totalIntegralLabel;//总积分
@property (weak, nonatomic) IBOutlet UILabel *withdrawIntegralLabel;//已提现积分
@property (weak, nonatomic) IBOutlet UILabel *notWithdrawIntegralLabel;//未提现积分

@property (weak, nonatomic) IBOutlet UIButton *rechargeButton;
@property (weak, nonatomic) IBOutlet UIButton *withdrawButton;

@property (nonatomic,strong) PersonalInfoModel *infoModel;
@end

@implementation MineViewController
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:animated];
    [self requestPersonalInfoData];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor colorWithHexString:@"#F5F5F5"];
    if (@available(iOS 11.0,*)) {
        self.scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    }else{
        self.automaticallyAdjustsScrollViewInsets=NO;
    }
    [self requestButtonStatusData];
}
- (void)requestButtonStatusData{
    [NetWorkTool POST:buttonHidden param:nil success:^(id dic) {
        NSString *status = dic[@"data"];
        if ([status isEqualToString:@"0"]) {
            self.rechargeButton.hidden = YES;
            self.withdrawButton.hidden = YES;
        } else {
            self.rechargeButton.hidden = NO;
            self.withdrawButton.hidden = NO;
        }
    } other:^(id dic) {
        
    } fail:^(NSError *error) {
        
    } needUser:YES];
}
- (void)requestPersonalInfoData{
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    param[@"userId"] = USER_ID;
    [NetWorkTool POST:getPersonal param:param success:^(id dic) {
        
        self.infoModel = [PersonalInfoModel mj_objectWithKeyValues:dic[@"data"]];
        [self.headImageView sd_setImageWithURL:[NSURL URLWithString:self.infoModel.img] placeholderImage:[UIImage imageNamed:@"默认头像"]];
        self.nameLabel.text = self.infoModel.nickname;
        if ([self.infoModel.isRealName isEqualToString:@"0"]) {
            self.accountStatusLabel.text = @"未实名认证";
        } else {
            self.accountStatusLabel.text = @"已认证";
        }
       // NSLog(@"%@",[USER_DEFAULT objectForKey:@"canSee"]);
        if ([[USER_DEFAULT objectForKey:@"canSee"] isKindOfClass:[NSNull class]] || [[USER_DEFAULT objectForKey:@"canSee"] isEqual:[NSNull null]] || [USER_DEFAULT objectForKey:@"canSee"] == nil || [[USER_DEFAULT objectForKey:@"canSee"] isEqualToString:@""] ) {
            
            self.totalMoneyLabel.text = [NSString stringWithFormat:@"￥%@",self.infoModel.balance];
            [self.canSeeButton setBackgroundImage:[UIImage imageNamed:@"睁眼"] forState:UIControlStateNormal];
            
        } else {
            self.totalMoneyLabel.text = @"****";
            [self.canSeeButton setBackgroundImage:[UIImage imageNamed:@"闭眼"] forState:UIControlStateNormal];
        
        }
        self.totalIntegralLabel.text = self.infoModel.integral;
        self.withdrawIntegralLabel.text = self.infoModel.outIntegral;
        self.notWithdrawIntegralLabel.text = self.infoModel.notIntegral;
        
       
    } other:^(id dic) {
    } fail:^(NSError *error) {
    } needUser:YES];
}
// 个人信息
- (IBAction)personalInfoButtonClick:(UIButton *)sender {
    
    InfoManageController * vc = [[InfoManageController alloc] init];
    vc.model = self.infoModel;
    [self.navigationController pushViewController:vc animated:YES];

}
// 可视按钮
- (IBAction)canSeeButtonClick:(UIButton *)sender {
    if ([[USER_DEFAULT objectForKey:@"canSee"] isKindOfClass:[NSNull class]] || [[USER_DEFAULT objectForKey:@"canSee"] isEqual:[NSNull null]] || [USER_DEFAULT objectForKey:@"canSee"] == nil || [[USER_DEFAULT objectForKey:@"canSee"] isEqualToString:@""] ){
        [self.canSeeButton setBackgroundImage:[UIImage imageNamed:@"闭眼"] forState:UIControlStateNormal];
        self.totalMoneyLabel.text = @"****";
        NSString *canSee = @"0";
        [USER_DEFAULT setObject:canSee forKey:@"canSee"];
        [USER_DEFAULT synchronize];
    } else {
        [self.canSeeButton setBackgroundImage:[UIImage imageNamed:@"睁眼"] forState:UIControlStateNormal];
        self.totalMoneyLabel.text = [NSString stringWithFormat:@"￥%@",self.infoModel.balance];
       
        [USER_DEFAULT removeObjectForKey:@"canSee"];
        [USER_DEFAULT synchronize];
    }
   // NSLog(@"===%@",[USER_DEFAULT objectForKey:@"canSee"]);
}
// 充值
- (IBAction)rechargeButtonClick:(UIButton *)sender {
    RechargeController * VC = [[RechargeController alloc] init];
    [self.navigationController pushViewController:VC animated:YES];
}
// 提现
- (IBAction)withdrawButtonClick:(UIButton *)sender {
    WithdrawController * VC = [[WithdrawController alloc] init];
    VC.model = self.infoModel;
    [self.navigationController pushViewController:VC animated:YES];
}

// 总积分
- (IBAction)totalIntegralButtonClick:(UIButton *)sender {
}

// 已提现积分
- (IBAction)withdrawIntegralButtonClick:(UIButton *)sender {
    WithdrawPointsController * vc = [[WithdrawPointsController alloc] init];
    vc.outIntegral = self.infoModel.outIntegral;
    [self.navigationController pushViewController:vc animated:YES];
}

// 未提现积分
- (IBAction)notWithdrawIntegralButtonClick:(UIButton *)sender {
    UnWithdrawPointsController * vc = [[UnWithdrawPointsController alloc] init];
    vc.model = self.infoModel;
    [self.navigationController pushViewController:vc animated:YES];
}

// 提货订单
- (IBAction)pickUpOrderButtonClick:(UIButton *)sender {
    PickUpOrderController * vc = [[PickUpOrderController alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
}

// 申请提货

- (IBAction)applyForDeliveryButtonClick:(UIButton *)sender {
    RequestPickUpController * vc = [[RequestPickUpController alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
    
}
// 收货地址
- (IBAction)shippingAddressButtonClick:(UIButton *)sender {
    ShippingAddressController * vc = [[ShippingAddressController alloc] init];
    vc.fromMineController = YES;
    [self.navigationController pushViewController:vc animated:YES];
}

// 消息
- (IBAction)newsButtonClick:(UIButton *)sender {
    MyMessageController * vc = [[MyMessageController alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
}

// 安全中心
- (IBAction)safeCenterButtonClick:(UIButton *)sender {
    SafeCenterController * vc = [[SafeCenterController alloc] init];
    vc.model = self.infoModel;
    [self.navigationController pushViewController:vc animated:YES];
}

// 推荐给好友
- (IBAction)recommendFriendsButtonClick:(UIButton *)sender {
    
    RecommendFriendsController * vc = [[RecommendFriendsController alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
}

// 我的好友
- (IBAction)myFriendsButtonClick:(UIButton *)sender {
    MyFriendsController * vc = [[MyFriendsController alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
}

// 更多
- (IBAction)moreButtonClick:(UIButton *)sender {
    MoreViewController * vc = [[MoreViewController alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
}
- (IBAction)loginOut:(UIButton *)sender {
    
    [USER_DEFAULT removeObjectForKey:@"user_id"];
    [USER_DEFAULT synchronize];
    ShowToastWithText(@"退出成功");
    [MBProgressHUD hideHUDForView:self.view animated:YES];
    //登录界面
    CodeLoginController *vc = [[CodeLoginController alloc]init];
    BaseNavViewController *nav = [[BaseNavViewController alloc]initWithRootViewController:vc];
    nav.modalPresentationStyle = UIModalPresentationFullScreen;
    [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:nav animated:YES completion:nil];
    
}

@end
